package com.codefish.framework.spring.core.io;

import com.codefish.framework.spring.core.utils.AssertUtils;
import com.codefish.framework.spring.core.utils.ClassUtils;

import java.net.MalformedURLException;

/**
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/09/30 下午 03:24
 */
public class DefaultResourceLoader implements ResourceLoader {

    @Override
    public Resource getResource(String location) {
        AssertUtils.notNull(location, "资源路径不能为空");
        //解析为classpath
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location, getClassLoader());
        }
        //解析为网络请求
        if (location.startsWith(HTTP_URL_PREFIX) || location.startsWith(HTTPS_URL_PREFIX)) {
            try {
                return new UrlResource(location);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        //其它路径，解析为文件系统路径
        return new FileSystemResource(location);
    }

    @Override
    public ClassLoader getClassLoader() {
        //获取默认的ClassLoader
        return ClassUtils.getDefaultClassLoader();
    }
}
